package servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.DBHelper;
import beans.Employer;
import beans.Employee;


/**
 * Servlet implementation class EmployeeMessage
 */
@WebServlet("/EmployeeMessage")
public class EmployeeMessage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EmployeeMessage() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    protected void handEmployeeMessage(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException{
    	
    	HttpSession session=request.getSession(false);
    	if(session.getAttribute("user")!=null){
	    	String employeeUsername= session.getAttribute("user").toString();
	    	    	
	    	Connection con = DBHelper.getConnection();
			PreparedStatement sql = DBHelper.getprepareStatement(con,"select providejob.company,providejob.salary,providejob.address,providejob.companysize,providejob.companytype,providejob.jobtype from providejob,applyjob where applyjob.jobid=providejob.jobid and applyjob.EMPLOYEEUSERNAME=? and applyjob.state = 'true'");
	    	sql.setString(1, employeeUsername);
	    	
	    	ResultSet rs = sql.executeQuery();
	    	
			ArrayList<Employee> employeeMessages=new ArrayList<Employee>();
			
			while(rs.next()){
	    		Employee employeeMessage = new Employee();
	
	    		employeeMessage.setCompany(rs.getString("company"));
	    		
	    		employeeMessage.setSalary(rs.getString("salary"));
	    		employeeMessage.setAddress(rs.getString("address"));
	    		employeeMessage.setCompanysize(rs.getString("companysize"));
	    		employeeMessage.setCompanytype(rs.getString("companytype"));
	    		employeeMessage.setJobtype(rs.getString("jobtype"));
	    		
	    		employeeMessages.add(employeeMessage);
			}
			
			request.setAttribute("employeeMessages", employeeMessages);
	    	
	    	request.getRequestDispatcher("employeeMessage.jsp").forward(request, response);
    	}
    	else
    		response.sendRedirect("login.jsp");
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			handEmployeeMessage(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			handEmployeeMessage(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
